#include "wnd_handle.h"
#include "wnd_main.h"
#include "mini_gui.h"
#include "wnd_set.h"
#include "wnd_dev_mng.h"





#if 0

MAIN_WND_HANDLE_S stGlbMainWndArray[] = {
	{
		.stAttr = {
			.s8Name = "主窗口",					/* 窗口的名称 本名称不会显示在窗口，只是调试打印时候用下*/
			//.u32WndId;					/* 主窗口的id*/
			//.u32ParentWndId;
			.enWndType = MAIN_WND_TYPE_MAIN_E,		/* 窗口的功能、类型*/
			.pstWnd = &stGlbMainWindow,
			.s32WndNum = 1,//sizeof(stGlbMainWindow) / sizeof(stGlbMainWindow[0]),

		},
		//MAIN_WND_HANDLE_FOPS_S stFops;

	},

	{
		.stAttr = {
			.s8Name = "设备管理",					/* 窗口的名称 本名称不会显示在窗口，只是调试打印时候用下*/
			//.u32WndId;					/* 主窗口的id*/
			//.u32ParentWndId;
			.enWndType = MAIN_WND_TYPE_DEV_MNG_E,		/* 窗口的功能、类型*/
			.pstWnd = &stGlbDevMngWindow,
			.s32WndNum = 1,//sizeof(stGlbMainWindow) / sizeof(stGlbMainWindow[0]),

		},
		//MAIN_WND_HANDLE_FOPS_S stFops;

	},
	{
		.stAttr = {
			.s8Name = "设置",					/* 窗口的名称 本名称不会显示在窗口，只是调试打印时候用下*/
			//.u32WndId;					/* 主窗口的id*/
			//.u32ParentWndId;
			.enWndType = MAIN_WND_TYPE_SET_E,		/* 窗口的功能、类型*/
			.pstWnd = &stGlbSetWindow,
			.s32WndNum = 1,//sizeof(stGlbMainWindow) / sizeof(stGlbMainWindow[0]),

		},
		//MAIN_WND_HANDLE_FOPS_S stFops;

	},

};

#if 0
#define WND_HANDLE_TYPE_MAIN		1000
WND_WIDGET_S stGlbMainWndHnadle[] = {
	{
		.enType = WND_WIDGET_TYPE_MAIN_E,
		//WND_WIDGET_ATTR_U unWidget;
		.stCommon = {
			.stAttr = {
				.s8Name = "主画面",
				.u32WndId = 0,
				.u32Group = WND_HANDLE_TYPE_MAIN,
				//.u32ParentWndId = HWND_DESKTOP,
				.u64Style = WS_VISIBLE | WS_BORDER,
				.u64ExStyle = WS_EX_NONE,
				.u32Id = 0,
				.u32WndX = 0,
				.u32WndY = 0,
				.u32WndWidth = UI_COORD_MAX,
				.u32WndHeight = UI_COORD_MAX,
				//.u32BkColor = COLOR_transparent,
				.bShowNow = true,
			},
			.stFops = {
				.CallBack = MINI_MainWndProc,
				//.Destroy)(struct wnd_widget_s *pstWidget);
			},
			//WND_STA_S stSta;
		},

		.pstWidget = stGlbMainWndCtrl,
		//.s32WidgetNum = sizeof(stGlbMainWndCtrl) / sizeof(stGlbMainWndCtrl[0]),
		.s32WidgetNum = s32MainWidgetNum,
	},

};
#endif





MAIN_MODULE_S stGlbMainModule = {
	.pstMainWndArray = stGlbMainWndArray,
	.s32MainWndNum = sizeof(stGlbMainWndArray) / sizeof(stGlbMainWndArray[0]),
};



MAIN_WND_HANDLE_S * MAIN_WND_Create(MAIN_MODULE_S *pstMainModule, MAIN_WND_TYPE_E enType,
										unsigned int u32ParentWndId,
										unsigned int u32BkColor)
{
	MAIN_WND_HANDLE_S *pstMainWndArray = pstMainModule->pstMainWndArray;
	int s32Num;
	WND_WIDGET_S *pstWnd = NULL;
	unsigned int u32Wnd;

	for(s32Num = 0; s32Num < pstMainModule->s32MainWndNum; s32Num++) {
		if(enType == pstMainWndArray[s32Num].stAttr.enWndType) {
			pstWnd = pstMainWndArray[s32Num].stAttr.pstWnd;
			if(pstWnd) {
				pstWnd->stCommon.stAttr.u32ParentWndId = u32ParentWndId;
				//pstWnd->stCommon.stAttr.enBkColor = u32BkColor;
				u32Wnd = WND_WIDGET_Create(pstWnd);
				if(u32Wnd == 0) {
					QLOG(ERR, "创建主窗口失败\n");
					return NULL;
				}
			}
		}
	}

	return pstMainWndArray;
}


void MAIN_WND_Destroy(MAIN_MODULE_S *pstMainModule, MAIN_WND_TYPE_E enType)
{
	MAIN_WND_HANDLE_S *pstMainWndArray = pstMainModule->pstMainWndArray;
	int s32Num;
	WND_WIDGET_S *pstWnd = NULL;

	for(s32Num = 0; s32Num < pstMainModule->s32MainWndNum; s32Num++) {
		if(enType == pstMainWndArray[s32Num].stAttr.enWndType) {
			pstWnd = pstMainWndArray[s32Num].stAttr.pstWnd;
			if(pstWnd) {
				//QLOG(TDBG, "删除[%s]\n", pstMainWndArray[s32Num].stAttr.s8Name);
				WND_WIDGET_Destroy(pstWnd);
			}
		}
	}

}



void MAIN_WND_DestroyAll(MAIN_MODULE_S *pstMainModule)
{
	MAIN_WND_HANDLE_S *pstMainWndArray = pstMainModule->pstMainWndArray;
	int s32Num;
	WND_WIDGET_S *pstWnd = NULL;

	for(s32Num = 0; s32Num < pstMainModule->s32MainWndNum; s32Num++) {
		pstWnd = pstMainWndArray[s32Num].stAttr.pstWnd;
		if(pstWnd) {
			WND_WIDGET_Destroy(pstWnd);
		}
	}
}






#endif



















/*!
 * 主窗口集合，在这里添加主窗口
 *
 */
WND_WIDGET_S *stGlbMainWndArry[] = {
    &stGlbMainWindow,   //!<	主窗口
    &stGlbSetWindow,    //!<	设置窗口
    &stGlbDevMngWindow, //!<	设备管理窗口
};




/*!
 * 主窗口集合头
 *
 */
WND_HANDLE_S stGlbMainWndHandle = {
    .enDefaultBkColour = UI_COLOR_BLACK_E,
    //.enDefaultFontColour = UI_COLOR_LIGHTWHITE_E,
    .pstWnd = stGlbMainWndArry,
    .s32WndNum = sizeof(stGlbMainWndArry)/ sizeof(stGlbMainWndArry[0]),
    .stDefaultFont = {
		.s32FontSize = 24,			/* 字体大小 0.默认大小， -1.跟随父窗口的大小*/
		//.enFontColor = UI_COLOR_LIGHTWHITE_E,
		.enFontColor = UI_COLOR_LIGHTWHITE_E,
	},
};



/*!
 * 创建一个主窗口
 * @param enMainType 要创建的组窗口的类型
 * @return 返回创建的主窗口的句柄
 * @see
 */
WND_WIDGET_S *WND_HANDLE_CreateMainWnd(MAIN_WND_TYPE_E enMainType)
{
	int s32Num;
	WND_HANDLE_S *pstMainWndHandle = &stGlbMainWndHandle;

	for(s32Num = 0; s32Num < pstMainWndHandle->s32WndNum; s32Num++) {
		if(pstMainWndHandle->pstWnd[s32Num]) {
			if(pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.u32Group == enMainType) {
				QLOG(TDBG, "创建 [%s]\n", pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.s8Name);
				WND_WIDGET_Create(pstMainWndHandle->pstWnd[s32Num], 0);
				return pstMainWndHandle->pstWnd[s32Num];
			}
		}
	}

	QLOG(ERR, "未找到主窗口，无法创建\n");
	return NULL;
}




static void * WND_HANDLE_DestroyProc(void *pArg)
{
	WND_WIDGET_S *pstWnd = (WND_WIDGET_S *)pArg;


	WND_WIDGET_Destroy(pstWnd);
	return NULL;
}

/*!
 * 销毁一个主窗口
 * @param enType 要销毁的主窗口的类型
 * @see
 */
void WND_HANDLE_Destroy(MAIN_WND_TYPE_E enType)
{

	WND_HANDLE_S *pstMainWndHandle = &stGlbMainWndHandle;

	int s32Num;
	for(s32Num = 0; s32Num < pstMainWndHandle->s32WndNum; s32Num++) {
		if(pstMainWndHandle->pstWnd[s32Num]) {
			if(pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.u32Group == enType) {
				QLOG(TDBG, "销毁 [%s]\n", pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.s8Name);
#if 1
				WND_WIDGET_Destroy(pstMainWndHandle->pstWnd[s32Num]);
#else
				if(pthread_create(&pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.stDestroyPid,
								NULL, WND_HANDLE_DestroyProc, (void*)pstMainWndHandle->pstWnd[s32Num])) {

					return ;
				}else {
					//线程设置成自动销毁
					if(pthread_detach(pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.stDestroyPid)) {
						return ;
					}

				}

#endif

			}
		}
	}
}


/*!
 * 销毁所有的主窗口
 * @see
 */
void WND_HANDLE_DestroyAll()
{
	WND_HANDLE_S *pstMainWndHandle = &stGlbMainWndHandle;

	int s32Num;
	for(s32Num = 0; s32Num < pstMainWndHandle->s32WndNum; s32Num++) {
		if(pstMainWndHandle->pstWnd[s32Num]) {
			QLOG(TDBG, "销毁 [%s]\n", pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.s8Name);
			WND_WIDGET_Destroy(pstMainWndHandle->pstWnd[s32Num]);
		}
	}
}


/*!
 * 找到主窗口
 * @param enMainType 根据类型找到主窗口
 * @return  返回找到的主窗口句柄
 * @see
 */
WND_WIDGET_S *WND_HANDLE_GetMainWnd(MAIN_WND_TYPE_E enMainType)
{
	int s32Num;
	WND_HANDLE_S *pstMainWndHandle = &stGlbMainWndHandle;

	for(s32Num = 0; s32Num < pstMainWndHandle->s32WndNum; s32Num++) {
		if(pstMainWndHandle->pstWnd[s32Num]) {
			if(pstMainWndHandle->pstWnd[s32Num]->stCommon.stAttr.u32Group == enMainType) {
				return pstMainWndHandle->pstWnd[s32Num];
			}
		}
	}

	QLOG(ERR, "未找到主窗口\n");
	return NULL;
}


